package com.thank.concurrency.samples.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 演示newScheduledThreadPool
 *      可以达到定时执行的线程
 */
@Slf4j
public class ThreadPoolExample4 {

    public static void main(String[] args) {

        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);

        // 延迟2秒后执行一次
        executorService.schedule(() -> log.info("schedule run"), 2, TimeUnit.SECONDS);

        // 延迟1秒后, 每隔三秒执行一次, 相当于定时循环执行
        executorService.scheduleAtFixedRate(() -> log.info("time schedule run..."), 1, 3, TimeUnit.SECONDS);

//        executorService.shutdown();

        // Timer也是一种定时任务, 复习一下
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("timer run...");
            }
        }, new Date());
    }
}
